//
//  googleChangePassWordDetailViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/10/26.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import SVProgressHUD
import Eureka
import SwiftyUserDefaults
//1.
//typealias swiftBlock1 = (_ str: String) -> Void

class googleChangePassWordDetailViewController: FormViewController {
    var changeStr : NSString! = ""
    var changeStr1 : NSString! = ""
    var changeStr2 : NSString! = ""
    var myuserPhone : NSString!
    var callBack: swiftBlock1?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
        makeBaseUI()
        // Do any additional setup after loading the view.
    }
    func makeBaseUI() {
        super.tableView.frame = CGRect(x: 15, y: 64, width: kScreenWidth - 30, height: kScreenHeight - 64)
        if #available(iOS 11.0.0, *) {
            super.tableView.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenHeight - 0)
        }
        
        let backImageView = UIImageView.init()
        backImageView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        backImageView.image = UIImage.init(named: "底色 拷贝 2@2x")
        self.view.addSubview(backImageView)
        self.view.sendSubview(toBack: backImageView)
        
        LabelRow.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        PushRow<String>.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        TextRow.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.backgroundColor = UIColor.clear
            cell.detailTextLabel?.textColor = UIColor.white
            cell.textField?.textColor = UIColor.white
            cell.titleLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        super.tableView.backgroundColor = UIColor.clear
        
        form +++ Section("")
            //为区域添加一个标签行
            
            <<< TextRow(XALanguageManager.getString(key: "passWorld")){
                $0.title = $0.tag
                $0.value = String(changeStr)
                }
                .cellSetup { cell, row in
                    row.add(rule: RuleMinLength(minLength: 6, msg:XALanguageManager.getString(key: "密码最低为六位")))
                    cell.textField.keyboardType = .twitter
                    row.validationOptions = .validatesOnChange
                    row.cellUpdate{ cell, se in
                        //                        self._phoneRow = row
                        if !row.isValid {
                            cell.titleLabel?.textColor = .red
                        }
                    }
                }
                .onChange({ (textField) in
                    if textField.value == nil{
                        self.changeStr = ""
                    }else{
                        self.changeStr = textField.value! as NSString
                    }
                    
                })
            <<<
            TextRow(XALanguageManager.getString(key: "makeSure password")){
                $0.title = $0.tag
                $0.value = String(changeStr1)
                }//选中标签行会在tittle后面加上" Summer "
                .cellSetup { cell, row in
                    row.add(rule: RuleMinLength(minLength: 6, msg:XALanguageManager.getString(key: "密码最低为六位")))
                    cell.textField.keyboardType = .twitter
                    row.validationOptions = .validatesOnChange
                    row.cellUpdate{ cell, se in
                        //                        self._phoneRow = row
                        if !row.isValid {
                            cell.titleLabel?.textColor = .red
                        }
                    }
                }
                .onChange({ (textField) in
                    if textField.value == nil{
                        self.changeStr1 = ""
                    }else{
                        self.changeStr1 = textField.value! as NSString
                    }
                    
                })
        <<<
        TextRow(XALanguageManager.getString(key: "google password")){
            $0.title = $0.tag
            $0.value = String(changeStr1)
            }//选中标签行会在tittle后面加上" Summer "
            .cellSetup { cell, row in
                row.add(rule: RuleMinLength(minLength: 6, msg:XALanguageManager.getString(key: "密码最低为六位")))
                cell.textField.keyboardType = .namePhonePad
                row.validationOptions = .validatesOnChange
                row.cellUpdate{ cell, se in
                    //                        self._phoneRow = row
                    if !row.isValid {
                        cell.titleLabel?.textColor = .red
                    }
                }
            }
            .onChange({ (textField) in
                if textField.value == nil{
                    self.changeStr2 = ""
                }else{
                    self.changeStr2 = textField.value! as NSString
                }
                
            })
        
    }
    
    func layoutNav(){
        self.title = XALanguageManager.getString(key: "修改")
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 44));
        btn1.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn1.setTitle(XALanguageManager.getString(key: "保存"), for: .normal)
        btn1.addTarget(self, action: #selector(googleChangePassWordDetailViewController.changeAction), for: .touchUpInside)
        btn1.setTitleColor(UIColor.white, for: .normal)
        let item2 = UIBarButtonItem(customView: btn1);        self.navigationItem.rightBarButtonItem = item2
    }
    //3.
    func callBackBlock(_ block: @escaping swiftBlock1) {
        
        callBack = block
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func chenageNextAction(){
        MyNetwork.request(target: .updateLoginpwd(user_id: Defaults[.nick_name]! as String, new_password: String().md5(inString: changeStr as String)), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                self.navigationController?.popToRootViewController(animated: true)
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    @objc func changeAction(){
        let errors = form.validate()
        if errors.count > 0 {
            let errorsString = errors.map({ (val) -> String in
                return val.msg
            }).joined(separator: "\n")
            SVProgressHUD.showError(withStatus: errorsString)
            return
        }
        
        if changeStr != changeStr1 {
            SVProgressHUD.showError(withStatus: "两次密码不一致")
            return
        }
        kLog(changeStr1)
        
        
        MyNetwork.request(target: .checkYanzhengqi(user_id: Defaults[.userID]!, code :self.changeStr2 as String), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                self.chenageNextAction()
            }else{
                SVProgressHUD.showError(withStatus: XALanguageManager.getString(key: "谷歌验证码错误"))
            }
        }) { (error) in
            kLog(error.localizedDescription)
        }
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
